﻿using System;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
using BCL.Entity;
using BCL.Repository;
using NazSoft.Framework;



public partial class UserControls_UCFeedback : System.Web.UI.UserControl
{
    public FeedbackType FeedbackType { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.FeedbackType == FeedbackType.Comments)
        {
            DescriptionLabel.Text = "Comments:";
            SubjectPlaceholder.Visible = false;
            txtSubject.Text = "Comments on News: ";
        }

        if (Request["popup"] == "t")
        {
            btnCancel.Attributes.Add("onclick", "javascript:parent.hideCommentsModal();return false");
        }
        else
        {
            btnCancel.Click += new EventHandler(btnCancel_Click);
        }
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {

        if (this.FeedbackType == global::FeedbackType.Feedback)
        {
            Feedback feedback = new Feedback();
            feedback.Name = txtName.Text;
            feedback.Subject = txtSubject.Text;
            feedback.Email = txtEmail.Text;
            feedback.Descriptions = txtDescription.Text;
            feedback.CreatedDate = DatetimeUtility.GetCurrentDateTime();

            FeedBackRepository repos = new FeedBackRepository();
            repos.Save(feedback);
        }

        SendEmailSelf();


        SendEmailToResponder();


        if (Request["popup"] == "t")
        {
            CancelButtonPlaceholder.Visible = true;
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(@"~/Default.aspx");
    }

    void SendEmailSelf()
    {
        // System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0

        // System.Net.Mail.SmtpClient is the alternate class for this in 2.0

        SmtpClient smtpClient = new SmtpClient();
        MailMessage message = new MailMessage();

        try
        {
            string type = this.FeedbackType == global::FeedbackType.Feedback ? "feedback" : "comment";


            // To address collection of MailAddress
            smtpClient.Credentials = new NetworkCredential("noreply@newsweek.net.pk", "azad");
            smtpClient.Host = "mail.newsweek.net.pk";
            //smtpClient.UseDefaultCredentials = true;
            //smtpClient.EnableSsl = true;


            MailAddress fromAddress = new MailAddress("noreply@newsweek.net.pk");
            message.From = fromAddress;


            //message.To.Add(new MailAddress(txtEmail.Text, txtName.Text));
            message.To.Add(new MailAddress("info@newsweek.net.pk", "NewsWeek"));


            message.Subject = "New " + type + " received: " + txtSubject.Text; //"Feedback";

            // CC and BCC optional
            // MailAddressCollection class is used to send the email to various users
            // You can specify Address as new MailAddress("admin1@yoursite.com")

            //message.CC.Add("khurram_khan123@hotmail.com");
            //message.CC.Add("zeeshanali@hotmail.com");


            // You can specify Address directly as string
            message.Bcc.Add(new MailAddress("khurram_khan123@hotmail.com"));
            message.Bcc.Add(new MailAddress("zeeshanali11@hotmail.com"));
            
            //Body can be Html or text format
            //Specify true if it  is html message
            message.IsBodyHtml = true;



            StringBuilder body = new StringBuilder();



            body.AppendLine("Hello,<br>You have received new " + type + " from " + txtName.Text + ".<br><br>");
            body.AppendLine("<div style=\"padding-left: 6px;border-left: 1px solid #CCC;\">");
            body.AppendLine("On " + DatetimeUtility.GetCurrentDateTime().ToString("D") + " at " + DatetimeUtility.GetCurrentDateTime().ToShortTimeString() + " <b>" + txtName.Text + " &lt;" + txtEmail.Text + "&gt;</b> wrote:<br>");
            body.AppendLine(txtDescription.Text);
            body.AppendLine("</div>");

            // Message body content
            message.Body = body.ToString();

            // Send SMTP mail

            smtpClient.Send(message);

            Panel1.Visible = false;

            ThankYouPanel.Visible = true;
            //lblStatus.Text = "Email successfully sent.";
        }
        catch (Exception ex)
        {
            //  Code that runs on application shutdown
            Elmah.Error error = new Elmah.Error(ex);
            //Elmah.ErrorLog.Default.Log(error);
            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(error);
            //lblStatus.Text = "Send Email Failed." + ex.Message;
        }



    }
    void SendEmailToResponder()
    {
        // System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0

        // System.Net.Mail.SmtpClient is the alternate class for this in 2.0

        SmtpClient smtpClient = new SmtpClient();
        MailMessage message = new MailMessage();

        try
        {
            string type = this.FeedbackType == global::FeedbackType.Feedback ? "feedback" : "comments";


            // To address collection of MailAddress
            smtpClient.Credentials = new NetworkCredential("noreply@newsweek.net.pk", "NewsWeek");
            smtpClient.Host = "mail.newsweek.net.pk";
            //smtpClient.UseDefaultCredentials = true;
            //smtpClient.EnableSsl = true;


            MailAddress fromAddress = new MailAddress("noreply@newsweek.net.pk");
            message.From = fromAddress;

            //fromAddress.DisplayName = "Daily azad riyast";


            message.To.Add(new MailAddress(txtEmail.Text, txtName.Text));
            
            message.Subject = "Thank you for your " + type + "!"; //"Feedback";

            // CC and BCC optional
            // MailAddressCollection class is used to send the email to various users
            // You can specify Address as new MailAddress("admin1@yoursite.com")

            //message.CC.Add("khurram_khan123@hotmail.com");
            //message.CC.Add("zeeshanali@hotmail.com");


            //Body can be Html or text format
            //Specify true if it  is html message
            message.IsBodyHtml = true;



            StringBuilder body = new StringBuilder();

            body.AppendFormat(@"Dear {0},<br><br>
                                        Thank you for your valuable {1}. If needed, we may get back to you.<br><br>
                                        Please feel free to let us know if you have additional questions or if we can be of any assistance.<br><br>
                                        Sincerely,<br><br>
                                        NewsWeek Team", txtName.Text, type);


            //body.AppendLine("Hello,<br>");
            //body.AppendLine("Thank you. We have received your feedback.");

            //body.AppendLine("<br><br>");
            //body.AppendLine("<div style=\"padding-left: 6px;border-left: 1px solid #CCC;\">");
            //body.AppendLine("On " + DatetimeUtility.GetCurrentDateTime().ToString("D") + " at " + DatetimeUtility.GetCurrentDateTime().ToShortTimeString() + " " + txtName.Text + " <" + txtEmail.Text + "> wrote:<br>");
            //body.AppendLine(txtDescription.Text);
            //body.AppendLine("</div>");

            // Message body content
            message.Body = body.ToString();

            // Send SMTP mail

            smtpClient.Send(message);

            Panel1.Visible = false;

            ThankYouPanel.Visible = true;
            //lblStatus.Text = "Email successfully sent.";
        }
        catch (Exception ex)
        {
            //  Code that runs on application shutdown
            Elmah.Error error = new Elmah.Error(ex);
            //Elmah.ErrorLog.Default.Log(error);
            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(error);
            //lblStatus.Text = "Send Email Failed." + ex.Message;
        }


    }
}